package com.yzy.controller;

import com.yzy.dao.DoctorDao;
import com.yzy.dao.PatientDao;
import com.yzy.entity.Doctors;
import com.yzy.utils.Utils;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DoctorLonginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String did,dpass;

        DoctorDao dao = new DoctorDao();
        int result=0;
        //1utf-8
        request.setCharacterEncoding("utf-8");
        //2读取请求体参数
        did = request.getParameter("doctorid");
        dpass = request.getParameter("doctorpass");
        ServletContext application = request.getServletContext();
        application.setAttribute("Dlongindid",did);
        //3调用dao
        //登录比较结果
        Utils util=new Utils();
        String sql="select count(*) from doctor where did=? and dpass=?";
        PreparedStatement ps=util.createStatement(sql);
        List<Doctors> doctorlist=new ArrayList<>();
        ResultSet rs = null;
        try {
            ps.setString(1,did);
            ps.setString(2,dpass);
             rs=ps.executeQuery();
            while (rs.next()){
                result=rs.getInt("count(*)");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs);
            util.close();
        }


        System.out.println(result);
        //4调用响应对象
        if(result==1){
            response.sendRedirect("/myWeb/doctor/book/find");


        }else{
            System.out.println(result);
            response.sendRedirect("/myWeb/adminlongin_error.html");
        }
    }
}
